from django.db import models
from django.contrib.auth.models import AbstractUser


class user_info(AbstractUser):
    telephone = models.CharField(max_length=11)
    icon = models.ImageField(upload_to='icon', default='default.png')

    def __str__(self):
        return self.username


class PricePolicy(models.Model):
    '''价格策略'''
    category_choice = ((1, '免费'),
                       (2, '收费'),
                       (3, '其他')
                       )
    category = models.SmallIntegerField(verbose_name='收费类型', choices=category_choice, default=2)
    title = models.CharField(verbose_name='标题', max_length=225)
    # PositiveIntegerField 正正数
    price = models.PositiveIntegerField(verbose_name='价格')

    project_count = models.PositiveIntegerField(verbose_name='项目个数')
    project_user = models.PositiveIntegerField(verbose_name='项目成员')
    project_space = models.PositiveIntegerField(verbose_name='项目空间', help_text='G')
    project_file = models.PositiveIntegerField(verbose_name='单个项目的空间', help_text='M')

    create_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title


class Transaction(models.Model):
    '''交易记录'''
    status_choices = (
        (1, '未支付'),
        (2, '已支付')
    )
    status = models.SmallIntegerField(verbose_name='支付状态', choices=status_choices, default=2)
    # 唯一订单号
    order = models.CharField(verbose_name='订单号', max_length=64, unique=True)

    user_info = models.ForeignKey(to='user_info')
    price_policy = models.ForeignKey(to='PricePolicy')

    num = models.IntegerField(verbose_name='购买年数', help_text='0表示无限')
    pay = models.IntegerField(verbose_name='实际支付', default=0)
    # 支付成功才加上时间（免费的没有时间）
    start_time = models.DateTimeField(verbose_name='开始时间', null=True, blank=True)
    end_time = models.DateTimeField(verbose_name='结束时间', null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class Project(models.Model):
    '''项目'''
    color_choice = (
        (1, "#56b8eb"),
        (2, "#f28033"),
        (3, "#ebc656"),
        (4, "#a2d148"),
        (5, "#20BFA4"),
        (6, "#7461c2"),
        (6, "#20bfa3"),
    )
    color = models.SmallIntegerField(verbose_name='颜色', choices=color_choice, default=1)
    name = models.CharField(verbose_name='项目名字', max_length=25)

    disc = models.CharField(verbose_name='项目描述', max_length=225, null=True, blank=True)
    star = models.BooleanField(verbose_name='星标', default=False)
    project_num = models.SmallIntegerField(verbose_name='项目人数', default=1)

    creator = models.ForeignKey(verbose_name='创建着', to='user_info')

    use_space = models.BigIntegerField(verbose_name='已使用的空间', default=0)
    create_time = models.DateTimeField(verbose_name='创见时间', auto_now_add=True)
    # 创建桶的名字
    bucket = models.CharField(max_length=255, verbose_name='桶名字')
    region = models.CharField(max_length=255, verbose_name='地区', default='ap-chengdu')

    def __str__(self):
        return self.name


class ProjectUser(models.Model):
    '''项目参与者'''
    project = models.ForeignKey(verbose_name='项目', to='Project', related_name='project')
    user = models.ForeignKey(verbose_name='用户', to='user_info', related_name='user_info')
    star = models.BooleanField(verbose_name='星标', default=False)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


'''
wiki表结构
id 标题 内容 项目(外键) 子关联(用做多级目录展示)
'''


class Wiki(models.Model):
    '''wiki文字'''
    title = models.CharField(verbose_name='标题', max_length=64)
    content = models.TextField(verbose_name='内容')
    # on_delete=models.CASCADE 项目没了，下面的文件全部删除
    project = models.ForeignKey(verbose_name='项目', to='Project', on_delete=models.CASCADE)
    # on_delete=models.CASCADE
    parent = models.ForeignKey(verbose_name='子关联', to='Wiki', related_name='children', null=True, blank=True,
                               on_delete=models.SET_NULL)
    depth = models.IntegerField(verbose_name='深度', default=1)

    def __str__(self):
        return self.title


class UploadFile(models.Model):
    type_choices = (
        (1, '文件'),
        (2, '文件夹')
    )
    name = models.CharField(verbose_name='名称', max_length=225)
    file_type = models.SmallIntegerField(verbose_name='文件类型', choices=type_choices, help_text='文件/文件夹')
    # 文件夹没有大小
    file_size = models.BigIntegerField(verbose_name='文件大小', null=True, blank=True, help_text='字节')
    # 父级没有parent id
    key = models.CharField(verbose_name='上传到cos的图片名字', max_length=225, null=True, blank=True)
    # 更新者
    update_user = models.ForeignKey(verbose_name='最近更新者', to='user_info')
    project = models.ForeignKey(verbose_name='项目', to='Project', related_name='upload_file')
    parent = models.ForeignKey(verbose_name='父目录', to='self', related_name='children', null=True, blank=True)
    # 自动更新 , auto_now_add=True(相当于创建时间) auto_now=True(自动更新当前，修改一次改变一次)
    update_datetime = models.DateTimeField(verbose_name='更新时间', auto_now=True)
